package net.hubt.itc.ui;



import net.hubt.itc.data.LookupCommand;

import net.hubt.itc.data.TimeTableData;
import net.hubt.itc.data.TimeTableXMLParser;
import net.hubt.itc.data.TimeTableList;

import net.hubt.itc.io.net.HttpUtils;

import net.hubt.itc.ui.utils.CTable;
import net.hubt.itc.util.LayoutUtils;
import net.hubt.itc.util.XMLDocument;
import android.app.AlertDialog;
import android.app.ProgressDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
//import android.util.Log;
import android.view.Gravity;

public class TimeTableActivity extends BaseActivity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStart() {
		super.onStart();
		executeGetTimeTableTask();
	}

	private void executeGetTimeTableTask() {
		/***********************************************************************
		 * 																	   *
		 *Programmer: Tiep, Nguyen											   *
		 *																	   *
		 *Last Modify: Nov, 23th, 2012										   *
		 *Add userClass to BaseActivity.java								   *
		 *Add CMD_TIMETABLE = 4 to LookupCommand.java  						   *
		 *Need to declare prepareParamsForTTbTask() to get userClass	  	   *																	   *
		 *Version: 1.0														   *
		 *																	   *
		 ***********************************************************************/	
				
		prepareParamsForTask();
		
		if (userClass != null){
			new GetTimeTableTask().execute(baseUrl + "/xml.php", "lop", userClass,
					"type", LookupCommand.CMD_TIMETABLE + "");
		}
		else {
			AlertDialog dialog=new AlertDialog.Builder(TimeTableActivity.this).create();
			dialog.setTitle(getString(R.string.ui_timetable_desc_short));
			//dialog.setMessage(getString(R.string.ui_trans_error_msg));			
			dialog.setMessage(getString(R.string.ui_timetable_noclass));
			dialog.setIcon(android.R.drawable.ic_dialog_info);
			dialog.setButton("OK", new DialogInterface.OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}); 
				
			dialog.show();	
		}
		
	}
	class GetTimeTableTask extends AsyncTask<String, String, String> {

		ProgressDialog dlgProgress;
		@Override
		protected void onPreExecute() {		
			super.onPreExecute();
			dlgProgress=ProgressDialog.show(TimeTableActivity.this, getString(R.string.app_global_load_title),
					getString(R.string.app_global_load_message),true);
		}
		@Override
		protected String doInBackground(String... addresses) {
			try {
				//connect to network, get xml data
				String timetableXmlData = HttpUtils.urlContentPost(addresses);
				SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(TimeTableActivity.this);
				preferences.edit().putString("timetableXmlData",timetableXmlData).commit();
				/*
				String temp=timetableXmlData;
				if(temp.equals(timetableXmlData))
				{
					preferences.edit().putString("boolean","true").commit();
				}
				else
				{
					preferences.edit().putString("boolean","false").commit();
				}
				*/
				return timetableXmlData;
			} catch (Exception ex) {
				//network error
				return "100";
			}
		}
		
		@Override
		protected void onProgressUpdate(String... values) {
		
		}

		@Override
		protected void onPostExecute(String timetableXmlData) {
			// tvContent.setText(transcriptXmlData);
			// xmlContent=result;
			//network error
			if(timetableXmlData.equalsIgnoreCase("100")){			
				dlgProgress.dismiss();
				AlertDialog dialog=new AlertDialog.Builder(TimeTableActivity.this).create();
				dialog.setTitle(getString(R.string.ui_timetable_desc_short));
				//dialog.setMessage(getString(R.string.ui_trans_error_msg));			
				dialog.setMessage(getString(R.string.ui_common_network_error_msg));
				dialog.setIcon(android.R.drawable.ic_dialog_info);
				dialog.setButton("OK", new DialogInterface.OnClickListener() {				
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}); 
					
				dialog.show();
			}			
			else{ // get xml data -> process and display
				processTimeTableData(timetableXmlData);
//				Log.e("Xml", scheduleXmlData);
				dlgProgress.dismiss();
			}
		}
	}
	protected void processTimeTableData(String xmlData) {
		XMLDocument xmlDoc = new XMLDocument();
		//Document document = xmlDoc.getDocument(xmlData);

		TimeTableXMLParser tp = new TimeTableXMLParser();
		tp.setRoot(xmlDoc.getRootNode(xmlData));

		TimeTableList studentTimeTable = tp.parseXmlData();
		if(studentTimeTable!=null){
			CTable timetableUI = buildTimeTableUI(this,
					studentTimeTable);
//			CTable timetableHeaderUI=buildTimeTableHeaderUI(this, studentTimeTable);
			
			setContentView(LayoutUtils.createLinearScrollableTableLayout(this,
					timetableUI,
					getString(R.string.ui_timetable_desc_short)
							+ userClass));
/*			setContentView(LayoutUtils.createLinearScrollableTableLayout(this,
					timetableUI,timetableHeaderUI));
*/		}
		else{ // can connect to server, but no data found
			AlertDialog dialog=new AlertDialog.Builder(TimeTableActivity.this).create();
			dialog.setTitle(getString(R.string.ui_schedule_desc_short));
			//dialog.setMessage(getString(R.string.ui_trans_error_msg));			
			dialog.setMessage(getString(R.string.ui_schedule_not_found_msg));
			dialog.setIcon(android.R.drawable.ic_dialog_info);
			dialog.setButton("OK", new DialogInterface.OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					startActivity(new Intent(TimeTableActivity.this, MainUI.class));
				}
			}); 
				
			dialog.show();					
		}

	}

	protected CTable buildTimeTableUI(Context context,
		TimeTableList studentTimeTable) {
		CTable timetable = new CTable(context);
		timetable.stretchColumn(0, true);
		timetable.stretchColumn(1, true);
		
		if (studentTimeTable != null) {
			int cnt = studentTimeTable.size();
			for (int i = 0; i < cnt; i++) {
				TimeTableData timetableObject = studentTimeTable
						.get(i);			
				
				timetable.addRow(timetable.makeColSpanRow(new int[]{Gravity.RIGHT},2, new boolean[]{true},getString(R.string.ui_timetable_thu) + timetableObject.getThu()));
				
				timetable.addRow(timetable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_timetable_ca),
						timetableObject.getCa()));
				
				timetable.addRow(timetable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_timetable_monhoc),
						timetableObject.getMonHoc()));
				timetable.addRow(timetable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_timetable_phonghoc),
						timetableObject.getPhongHoc()));

				
			}
		}		
		return timetable;
	}
}




